package com.zf.shuashua.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meis.base.mei.adapter.MeiBaseAdapter;
import com.meis.base.mei.base.BaseListFragment;
import com.meis.base.mei.entity.Result;
import com.meis.base.mei.utils.ParseJsonUtils;
import com.zf.shuashua.R;
import com.zf.shuashua.api.AbsCallback;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.bean.FansList;
import com.zf.shuashua.bean.FocusType;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import io.reactivex.Observable;


public class FansFragment extends BaseListFragment<FansList.DataBean> {
    private int refresh;
    private FansFragmVM VM;
    private RecyclerView rv;
    private FansListAdapter mAdapter;
    private int page = 1;
    private String user_id;
//    private ListenerList listener;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_focus;
    }

    @Override
    protected void initView() {
        View view = setEmptyLayout(R.layout.empty_feed_list);
        if (getArguments() == null) {
            return;
        }
        user_id = getArguments().getString("user_id");
        refresh = getArguments().getInt("refresh");
        if (!TextUtils.isEmpty(user_id)) {
            ((TextView) view.findViewById(R.id.emptyTitle)).setText("该作者还没有粉丝~");
        }
        super.initView();
    }

    public static FansFragment newInstance(int refresh, String user_id) {
        Bundle args = new Bundle();
        args.putInt("refresh", refresh);
        args.putString("user_id", user_id);
        FansFragment fragment = new FansFragment();
        fragment.setArguments(args);
        return fragment;
    }

//    public void onAttach(Context context) {
//        super.onAttach(context);
//        try {
//            listener = (ListenerList) context;
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    public void update() {
//        page = 1;
//        VM.getDataRefresh(user_id, page);
    }


    @Override
    protected RecyclerView getRecyclerView() {
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(mActivity));
        return rv;
    }

    @Override
    protected MeiBaseAdapter<FansList.DataBean> getAdapter() {
        mAdapter = new FansListAdapter(R.layout.fans_item, null);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            FansList.DataBean dataBean = (FansList.DataBean) adapter.getData().get(position);
            if (dataBean.getFans() != null) {
                HttpData.getInstance().getFocusType(dataBean.getFans().getId(), new AbsCallback<BaseData<FocusType>>() {
                    @Override
                    public void onRealSuccess(BaseData<FocusType> data) {
                        switch (data.getData().getFollow_status()) {
                            case 0://未关注
                                view.setBackgroundResource(R.drawable.fans_off);
                                ((TextView) view).setText("关注");
                                break;
                            case 1://已关注
                            case 2://相互关注
                                view.setBackgroundResource(R.drawable.fans_no);
                                ((TextView) view).setText(data.getData().getFollow_status_text());
                                break;
                        }
//                        if (listener != null) {
//                            listener.listener(refresh);
//                        }
                        mActivity.setResult(RESULT_OK);
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        LogUtil.d(errMsg);

                    }
                });


            }
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            FansList.DataBean dataBean = (FansList.DataBean) adapter.getData().get(position);
            if (dataBean.getFans() != null) {
                mActivity.startActivity(new Intent(mActivity, FriendsDetailsActivity.class).putExtra("USER_ID", dataBean.getFans().getId() + ""));
            }
        });
        return mAdapter;
    }

    @Override
    protected Observable<Result<List<FansList.DataBean>>> getListObservable(int pageNo) {
        Observable<String> observable = HttpData.getInstance().getFansList(user_id, pageNo);
        return getListData(observable);
    }

    private Observable<Result<List<FansList.DataBean>>> getListData(Observable<String> observable) {
        final Result<List<FansList.DataBean>> result = new Result<>();
        return observable.map(s -> {
            if (!TextUtils.isEmpty(s)) {
                try {
                    FansList fansList = ParseJsonUtils.parseData(s, FansList.class);
                    result.data = fansList.getData();
                } catch (Exception e) {
                }
            }
            return result;
        });
    }


    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canPullToRefresh() {
        return true;
    }
}